﻿//## Microsoft Reciprocal License (Ms-RL)
//##
//## This license governs use of the accompanying software. If you use the software, you accept this license. If you do not accept the license, do not use the software.
//##
//## 1. Definitions
//##
//## The terms "reproduce," "reproduction," "derivative works," and "distribution" have the same meaning here as under U.S. copyright law.
//##
//## A "contribution" is the original software, or any additions or changes to the software.
//##
//## A "contributor" is any person that distributes its contribution under this license.
//## 
//## "Licensed patents" are a contributor's patent claims that read directly on its contribution.
//## 
//## 2. Grant of Rights
//## 
//## (A) Copyright Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free copyright license to reproduce its contribution, prepare derivative works of its contribution, and distribute its contribution or any derivative works that you create.
//## 
//## (B) Patent Grant- Subject to the terms of this license, including the license conditions and limitations in section 3, each contributor grants you a non-exclusive, worldwide, royalty-free license under its licensed patents to make, have made, use, sell, offer for sale, import, and/or otherwise dispose of its contribution in the software or derivative works of the contribution in the software.
//## 
//## 3. Conditions and Limitations
//## 
//## (A) Reciprocal Grants- For any file you distribute that contains code from the software (in source code or binary format), you must provide recipients the source code to that file along with a copy of this license, which license will govern that file. You may license other files that are entirely your own work and do not contain code from the software under any terms you choose.
//## 
//## (B) No Trademark License- This license does not grant you rights to use any contributors' name, logo, or trademarks.
//## 
//## (C) If you bring a patent claim against any contributor over patents that you claim are infringed by the software, your patent license from such contributor to the software ends automatically.
//## 
//## (D) If you distribute any portion of the software, you must retain all copyright, patent, trademark, and attribution notices that are present in the software.
//## 
//## (E) If you distribute any portion of the software in source code form, you may do so only under this license by including a complete copy of this license with your distribution. If you distribute any portion of the software in compiled or object code form, you may only do so under a license that complies with this license.
//## 
//## (F) The software is licensed "as-is." You bear the risk of using it. The contributors give no express warranties, guarantees or conditions. You may have additional consumer rights under your local laws which this license cannot change. To the extent permitted under your local laws, the contributors exclude the implied warranties of merchantability, fitness for a particular purpose and non-infringement.  

// Written by: Roman D. Clarkson - tunning by Fabian Figueredo
// http://www.romanclarkson.com  inspirit@romanclarkson.com


#region Using

using System;
using System.Xml;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration.Provider;
using System.Web.Security;
using System.Web.Hosting;
using System.Web.Management;
using System.Security.Permissions;
using System.Web;
using System.Text;
using System.Security.Cryptography;
using System.Configuration;
using AFDS.Core;
#endregion

namespace AFDS.DataAccess.Providers.xml
{
    ///<summary>
    ///</summary>
    public class XmlRoleProvider : RoleProvider
    {        
        #region Properties
        private List<IRole> _Roles = new List<IRole>();
        private List<string> _UserNames;
        private string _XmlFileName;
        private string[] _DefaultRolesToAdd;
        private string roleadm;
        private string storage;


        ///<summary>
        ///Gets or sets the name of the application to store and retrieve role information for.
        ///</summary>
        ///
        ///<returns>
        ///The name of the application to store and retrieve role information for.
        ///</returns>
        ///
        public override string ApplicationName
        {
            get { return "AFDS"; }
            set { }
        }


        public string RoleAdm
        {
            get {
                if (string.IsNullOrEmpty(roleadm))
                    return ConfigurationManager.AppSettings["RolAdminName"];
                else
                    return roleadm; }
            set { roleadm = value; }
        }


        public string Storage
        {
            get {
                if (string.IsNullOrEmpty(storage))
                    return ConfigurationManager.AppSettings["StorageLocation"];
                else
                    return storage;
            }
            set { storage = value; }
        }        

        ///<summary>
        ///Gets a value indicating whether the specified role name already exists in the role data source for the configured applicationName.
        ///</summary>
        ///
        ///<returns>
        ///true if the role name already exists in the data source for the configured applicationName; otherwise, false.
        ///</returns>
        ///
        ///<param name="roleName">The name of the role to search for in the data source. </param>
        public override bool RoleExists(string roleName)
        {
            List<string> currentRoles = new List<string>(GetAllRoles());
            return (currentRoles.Contains(roleName)) ? true : false;
        }

        ///<summary>
        ///Gets a list of all the roles for the configured applicationName.
        ///</summary>
        ///
        ///<returns>
        ///A string array containing the names of all the roles stored in the data source for the configured applicationName.
        ///</returns>
        ///
        public override string[] GetAllRoles()
        {
            List<string> allRoles = new List<string>();
            foreach (IRole role in _Roles)
            {
                allRoles.Add(role.Name);
            }
            return allRoles.ToArray();
        }

        ///<summary>
        ///Gets a list of users in the specified role for the configured applicationName.
        ///</summary>
        ///
        ///<returns>
        ///A string array containing the names of all the users who are members of the specified role for the configured applicationName.
        ///</returns>
        ///
        ///<param name="roleName">The name of the role to get the list of users for. </param>
        public override string[] GetUsersInRole(string roleName)
        {
            //  ReadRoleDataStore();
            List<string> UsersInRole = new List<string>();

            foreach (IRole role in _Roles)
            {
                if (role.Name.Equals(roleName, StringComparison.OrdinalIgnoreCase))
                {
                    foreach (string user in role.Users)
                    {
                        UsersInRole.Add(user.ToLowerInvariant());
                    }
                }
            }
            return UsersInRole.ToArray();
        }

        ///<summary>
        ///Gets a value indicating whether the specified user is in the specified role for the configured applicationName.
        ///</summary>
        ///
        ///<returns>
        ///true if the specified user is in the specified role for the configured applicationName; otherwise, false.
        ///</returns>
        ///
        ///<param name="username">The user name to search for.</param>
        ///<param name="roleName">The role to search in.</param>
        public override bool IsUserInRole(string username, string roleName)
        {
            foreach (IRole role in _Roles)
            {
                if (role.Name.Equals(roleName, StringComparison.OrdinalIgnoreCase))
                {
                    foreach (string user in role.Users)
                    {
                        if (user.Equals(username, StringComparison.OrdinalIgnoreCase))
                            return true;
                    }
                }
            }
            return false;
        }

        ///<summary>
        ///Gets a list of the roles that a specified user is in for the configured applicationName.
        ///</summary>
        ///
        ///<returns>
        ///A string array containing the names of all the roles that the specified user is in for the configured applicationName.
        ///</returns>
        ///
        ///<param name="username">The user to return a list of roles for.</param>
        public override string[] GetRolesForUser(string username)
        {
            //  ReadRoleDataStore();
            List<string> rolesForUser = new List<string>();

            foreach (IRole role in _Roles)
            {
                foreach (string user in role.Users)
                {
                    if (user.Equals(username, StringComparison.OrdinalIgnoreCase))
                        rolesForUser.Add(role.Name);
                }
            }
            return rolesForUser.ToArray();
        }

        #endregion

        #region Supported methods

        ///<summary>
        ///Gets an array of user names in a role where the user name contains the specified user name to match.
        ///</summary>
        ///
        ///<returns>
        ///A string array containing the names of all the users where the user name matches usernameToMatch and the user is a member of the specified role.
        ///</returns>
        ///
        ///<param name="usernameToMatch">The user name to search for.</param>
        ///<param name="roleName">The role to search in.</param>
        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            List<string> UsersInRole = new List<string>();
            if (IsUserInRole(usernameToMatch, roleName))
                UsersInRole.AddRange(_UserNames);
            return UsersInRole.ToArray();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="config"></param>
        public override void Initialize(string name, NameValueCollection config)
        {

            if (string.IsNullOrEmpty(RoleAdm))
                throw new Exception("RoleAdmId is null or empty");
                       
            _DefaultRolesToAdd = new string[] { RoleAdm };           

            ReadMembershipDataStore();

            if (config == null)
                throw new ArgumentNullException("config");

            if (String.IsNullOrEmpty(name))
                name = "XmlMembershipProvider";

            if (Type.GetType("Mono.Runtime") != null)
            {
                // Mono dies with a "Unrecognized attribute: description" if a description is part of the config.
                if (!string.IsNullOrEmpty(config["description"]))
                {
                    config.Remove("description");
                }
            }
            else
            {
                if (string.IsNullOrEmpty(config["description"]))
                {
                    config.Remove("description");
                    config.Add("description", "XML role provider");
                }
            }

            base.Initialize(name, config);

            // Initialize _XmlFileName and make sure the path
            // is app-relative
            string path = config["xmlFileName"];

            if (String.IsNullOrEmpty(path))
                path = "~/App_Data/roles.xml";

            if (!VirtualPathUtility.IsAppRelative(path))
                throw new ArgumentException
                    ("xmlFileName must be app-relative");

            string fullyQualifiedPath = VirtualPathUtility.Combine
                (VirtualPathUtility.AppendTrailingSlash
                     (HttpRuntime.AppDomainAppVirtualPath), path);

            _XmlFileName = HostingEnvironment.MapPath(fullyQualifiedPath);
            config.Remove("xmlFileName");

            // Make sure we have permission to read the XML data source and
            // throw an exception if we don't
            FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.Write, _XmlFileName);
            permission.Demand();

            if (!System.IO.File.Exists(_XmlFileName))
                AddUsersToRoles(_UserNames.ToArray(), _DefaultRolesToAdd);

            //Now that we know a xml file exists we can call it.
            ReadRoleDataStore();

            if (!RoleExists(RoleAdm))
                AddUsersToRoles(_UserNames.ToArray(), _DefaultRolesToAdd);



            // Throw an exception if unrecognized attributes remain
            if (config.Count > 0)
            {
                string attr = config.GetKey(0);
                if (!String.IsNullOrEmpty(attr))
                    throw new ProviderException("Unrecognized attribute: " + attr);
            }


        }

        ///<summary>
        ///Adds the specified user names to the specified roles for the configured applicationName.
        ///</summary>
        ///
        ///<param name="roleNames">A string array of the role names to add the specified user names to. </param>
        ///<param name="usernames">A string array of user names to be added to the specified roles. </param>
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            List<string> currentRoles = new List<string>(GetAllRoles());
            if (usernames.Length != 0 && roleNames.Length != 0)
            {
                foreach (string _rolename in roleNames)
                {
                    if (!currentRoles.Contains(_rolename))
                    {
                        _Roles.Add(new RoleBuilder().BuildRole(_rolename, new List<string>(usernames)));
                    }
                }

                foreach (IRole role in _Roles)
                {
                    foreach (string _name in roleNames)
                    {
                        if (role.Name.Equals(_name, StringComparison.OrdinalIgnoreCase))
                        {
                            foreach (string s in usernames)
                            {
                                if (!role.Users.Contains(s))
                                    role.Users.Add(s);
                            }
                        }
                    }
                }
            }
            Save();
        }

        ///<summary>
        ///Removes the specified user names from the specified roles for the configured applicationName.
        ///</summary>
        ///
        ///<param name="roleNames">A string array of role names to remove the specified user names from. </param>
        ///<param name="usernames">A string array of user names to be removed from the specified roles. </param>
        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            if (usernames.Length != 0 && roleNames.Length != 0)
            {
                foreach (Role role in _Roles)
                {
                    foreach (string _name in roleNames)
                    {
                        if (role.Name.Equals(_name, StringComparison.OrdinalIgnoreCase))
                        {
                            foreach (string user in usernames)
                            {
                                if (role.Name.Equals(RoleAdm, StringComparison.OrdinalIgnoreCase))
                                {
                                    if (role.Users.Count != 1)
                                    {
                                        //if (role.Users.Contains(user))
                                        //    role.Users.Remove(user);
                                        RemoveItemFromList(role.Users, user);
                                    }
                                }
                                else
                                {
                                    //if (role.Users.Contains(user))
                                    //    role.Users.Remove(user);
                                    RemoveItemFromList(role.Users, user);
                                }
                            }
                            break;
                        }
                    }
                }
            }
            Save();
        }

        private static void RemoveItemFromList(ICollection<string> list, string item)
        {
            if (list == null || string.IsNullOrEmpty(item) || list.Count == 0)
                return;

            List<string> usersToRemove = new List<string>();

            foreach (string u in list)
            {
                if (u.Equals(item, StringComparison.OrdinalIgnoreCase))
                    usersToRemove.Add(u);
            }
            foreach (string u in usersToRemove)
            {
                list.Remove(u);
            }
        }

        ///<summary>
        ///Removes a role from the data source for the configured applicationName.
        ///</summary>
        ///
        ///<returns>
        ///true if the role was successfully deleted; otherwise, false.
        ///</returns>
        ///
        ///<param name="throwOnPopulatedRole">If true, throw an exception if roleName has one or more members and do not delete roleName.</param>
        ///<param name="roleName">The name of the role to delete.</param>
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            if (!roleName.Equals(RoleAdm, StringComparison.OrdinalIgnoreCase))
            {
                _Roles.Remove(new AFDS.Core.RoleBuilder().BuildRole(roleName));
                Save();
                return true;
            }
            return false;
        }

        ///<summary>
        ///Adds a new role to the data source for the configured applicationName.
        ///</summary>
        ///
        ///<param name="roleName">The name of the role to create.</param>
        public override void CreateRole(string roleName)
        {
            IRole role = new AFDS.Core.RoleBuilder().BuildRole(roleName);
            if (!_Roles.Contains(role))
            {
                _Roles.Add(role);
                Save();
            }

        }

        #endregion

        #region Helper methods

        /// <summary>
        /// Builds the internal cache of users.
        /// </summary>
        private void ReadRoleDataStore()
        {
            lock (this)
            {
                XmlDocument doc = new XmlDocument();

                try
                {
                    doc.Load(_XmlFileName);
                    XmlNodeList nodes = doc.GetElementsByTagName("role");
                    foreach (XmlNode roleNode in nodes)
                    {
                        IRole tempRole = new AFDS.Core.RoleBuilder().BuildRole(roleNode.SelectSingleNode("name").InnerText);
                        foreach (XmlNode userNode in roleNode.SelectNodes("users/user"))
                        {
                            tempRole.Users.Add(userNode.InnerText);
                        }
                        _Roles.Add(tempRole);

                    }
                }
                catch (XmlException)
                {
                    AddUsersToRoles(_UserNames.ToArray(), _DefaultRolesToAdd);
                }

            }
        }

        ///<summary>
        ///</summary>
        public void Save()
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            using (XmlWriter writer = XmlWriter.Create(_XmlFileName, settings))
            {
                writer.WriteStartDocument(true);
                writer.WriteStartElement("roles");

                foreach (IRole _role in _Roles)
                {
                    writer.WriteStartElement("role");
                    writer.WriteElementString("name", _role.Name);
                    writer.WriteStartElement("users");
                    foreach (string username in _role.Users)
                    {
                        writer.WriteElementString("user", username);
                    }
                    writer.WriteEndElement(); //closes users
                    writer.WriteEndElement(); //closes role
                }
            }

        }

        /// <summary>
        /// Only so we can add users to the adminstrators role.
        /// </summary>
        private void ReadMembershipDataStore()
        {            
            string fullyQualifiedPath = VirtualPathUtility.Combine
              (VirtualPathUtility.AppendTrailingSlash
              (HttpRuntime.AppDomainAppVirtualPath), Storage + "roles.xml");

            lock (this)
            {
                if (_UserNames == null)
                {
                    _UserNames = new List<string>();
                    XmlDocument doc = new XmlDocument();
                    doc.Load(HostingEnvironment.MapPath(fullyQualifiedPath));
                    XmlNodeList nodes = doc.GetElementsByTagName("User");

                    foreach (XmlNode node in nodes)
                    {
                        _UserNames.Add(node["UserName"].InnerText);
                    }

                }
            }
        }
        #endregion
    }
}